#include <bits/stdc++.h>

using namespace std;
const int N=70;
int n,m,k,sum=0;
int a[N][N]={0};
int main()
{
	cin >> n >> m >> k;
	int t = m / 2;
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; j++)
		{
			cin >> a[i][j];
			sort(a, a+i);
		}
	}
	for (int i = n; i >= n - t; i--)
	{
		for (int j = 1; j <= m; j++)
		{
			sum += a[i][j];
		}
	}
	if (sum % k == 0)
	{
		cout << sum;
	}
	else
	{
		int y = sum % k;
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= m; j++)
			{
				if (y == a[i][j])
				{
					cout << sum - y;
				}
				else if(y+k==a[i][j])
				{
					
				}
			}
		}
	}
	return 0;
}